home *** CD-ROM | disk | FTP | other *** search
/ FM Towns: Free Software Collection 10 / FM Towns Free Software Collection 10.iso / ms_dos / tool / dat2c / dat2c.c next >
Text File  |  1995-02-07  |  1KB  |  70 lines

  1. #include <stdio.h>
  2.  
  3. #define NOERR    0
  4. #define ERR        1
  5.  
  6. char    hexchar[17] = "0123456789ABCDEF";
  7.  
  8. void    hex2( char *p, int i )
  9. {
  10.     *(p+0) = '0';
  11.     *(p+1) = 'x';
  12.     *(p+2) = hexchar[ ( i & 0xf0 ) >> 4 ];
  13.     *(p+3) = hexchar[ ( i & 0x0f )      ];
  14.     *(p+4) = 0;
  15.  
  16.     return;
  17. }
  18.  
  19. int        main( int argc, char *argv[] )
  20. {
  21.     FILE    *fin,*fout;
  22.     int        i,c;
  23.     char    buf[16];
  24.  
  25.     if( argc < 4 )
  26.     {
  27.         printf( "usage : dat2c [InputDataFile] [OutputC-SourceFile] [変数名]\n" );
  28.         return ERR;
  29.     }
  30.  
  31.     if( ( fin = fopen( argv[1], "rb" ) ) == NULL )
  32.     {
  33.         printf( "%s のオープンに失敗しました。\n", argv[1] );
  34.         return ERR;
  35.     }
  36.  
  37.     if( ( fout = fopen( argv[2], "w" ) ) == NULL )
  38.     {
  39.         printf( "%s のオープンに失敗しました。\n", argv[2] );
  40.         return ERR;
  41.     }
  42.  
  43.     fputs( "char\t", fout );
  44.     fputs( argv[3], fout );
  45.     fputs( " = {", fout );
  46.  
  47.     i = 0;
  48.     while(1)
  49.     {
  50.         c = fgetc( fin );
  51.         if( feof( fin ) )
  52.             break;
  53.         if( i )
  54.             fputs( ",", fout );
  55.         if( (i & 7) == 0 )
  56.             fputs( "\n", fout );
  57.         ++ i;
  58.         hex2( buf, c );
  59.         fputs( " ", fout );
  60.         fputs( buf, fout );
  61.     }
  62.  
  63.     fputs( "\n};\n", fout );
  64.  
  65.     fclose( fin );
  66.     fclose( fout );
  67.  
  68.     return NOERR;
  69. }
  70.